﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Codejam2014.R1B
{
    class Program
    {
        class Sample
        {
            public bool fail = false;
            public SortedList<int, List<int>> charCntList = new SortedList<int, List<int>>();
        }

        static void Main(string[] args)
        {
            string sampleName = "large";
            string sampleDir = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory))) + "\\";
            string iFilePath = sampleDir + sampleName + ".i.txt";
            string oFilePath = sampleDir + sampleName + ".o.txt";
            string cFilePath = sampleDir + sampleName + ".c.txt";

            string[] iLines = File.ReadAllLines(iFilePath);
            List<string> oLines = new List<string>();

            int lineCnt = 0;
            int sampleCnt = int.Parse(iLines[lineCnt++]);

            List<Sample> sampleList = new List<Sample>();
            for (int sampleIdx = 0; sampleIdx < sampleCnt; sampleIdx++)
            {
                int itemCnt = int.Parse(iLines[lineCnt++]);

                Sample s = new Sample();
                string prevTrimmedStr = "";
                for (int i = 0; i < itemCnt; i++)
                {
                    string str = iLines[lineCnt++];

                    string trimmedStr = "";

                    char prev = ' ';
                    int cnt = 0;
                    for ( int j = 0 ; j < str.Length ; j++ ) {
                        char curr = str[j];
                        if (curr != prev && cnt > 0)
                        {
                            if (!s.charCntList.ContainsKey(trimmedStr.Length)) s.charCntList[trimmedStr.Length] = new List<int>();
                            s.charCntList[trimmedStr.Length].Add(cnt);
                            trimmedStr += prev;
                            cnt = 0;
                        }
                        cnt++;
                        prev = curr;
                    }
                    if (cnt > 0)
                    {
                        trimmedStr += prev;
                        if (!s.charCntList.ContainsKey(prev)) s.charCntList[prev] = new List<int>();
                        s.charCntList[prev].Add(cnt);
                        cnt = 0;
                    }
                    if ( prevTrimmedStr != "" && prevTrimmedStr != trimmedStr ) {
                        s.fail = true;
                    }
                    prevTrimmedStr = trimmedStr;
                }
                sampleList.Add(s);
            }

            for (int sampleIdx = 0; sampleIdx < sampleCnt; sampleIdx++)
            {
                Sample s = sampleList[sampleIdx];
                if ( s.fail ) {
                    oLines.Add(string.Format("Case #{0}: Fegla Won", sampleIdx + 1));
                    continue;
                }

                int distance = 0;
                foreach (var kv in s.charCntList)
                {
                    int avg = (int)Math.Round(kv.Value.Average());
                    foreach (int cnt in kv.Value)
                    {
                        distance += Math.Abs(cnt - avg);
                    }
                }
                oLines.Add(string.Format("Case #{0}: {1}", sampleIdx + 1, distance));
            }
            File.WriteAllLines(oFilePath, oLines);

            if (File.Exists(cFilePath))
            {
                string[] cLines = File.ReadAllLines(cFilePath);
                for (int sampleIdx = 0; sampleIdx < sampleCnt; sampleIdx++)
                {
                    string oLine = oLines[sampleIdx];
                    string cLine = cLines[sampleIdx];

                    if (oLine != cLine)
                    {
                        throw new Exception("not match");
                    }
                }
            }
        }
    }
}
